from django.db import models


# 基类
class BaseModel(models.Model):
    # auto_now_add = True   创建时自动添加的时间
    created_time = models.DateTimeField("创建时间", auto_now_add=True)
    # auto_now = Ture   更新数据时  修改时间
    updated_time = models.DateTimeField("更新时间", auto_now=True)
    logic_del = models.SmallIntegerField("逻辑删除", default=0)

    class Meta:
        abstract = True


# 部门表
class DepartmentModel(BaseModel):
    d_name = models.CharField('部门名称', max_length=18)
    d_comment = models.CharField('部门备注', max_length=320, null=True, default=None)
    assoc = models.ForeignKey("self", verbose_name='自关联', on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_department'

    def __str__(self):
        return self.d_name


# 职级表
class RankModel(BaseModel):
    r_name = models.CharField('职级名称', max_length=26)
    r_comment = models.CharField('职级备注', max_length=320, null=True, default=None)

    class Meta:
        db_table = 'tb_rank'

    def __str__(self):
        return self.r_name


# 岗位表
class JobsModel(BaseModel):
    j_name = models.CharField('岗位名称', max_length=32)
    j_comment = models.CharField('岗位备注', max_length=320, null=True, default=None)

    class Meta:
        db_table = 'tb_jobs'

    def __str__(self):
        return self.j_name


# 岗位职级表
class RankJobsModel(BaseModel):
    rank = models.ForeignKey(RankModel, verbose_name='职级外键', on_delete=models.CASCADE)
    jobs = models.ForeignKey(JobsModel, verbose_name='岗位外键', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_rank_jobs'

    def __str__(self):
        return self.rank


# 角色表
class RoleModel(BaseModel):
    name = models.CharField('角色名', max_length=32)
    comment = models.CharField('角色备注', max_length=320)

    class Meta:
        db_table = 'tb_role'

    def __str__(self):
        return self.name


# 菜单表
class MenuModel(BaseModel):
    m_name = models.CharField('菜单名称', max_length=10)
    m_route = models.CharField('菜单路径', max_length=50,null=True)
    icon = models.CharField("菜单图标", max_length=50,null=True)
    m_typ = models.CharField('菜单类型', max_length=32)
    m_sort = models.IntegerField('排序')
    assoc = models.ForeignKey("self", verbose_name='自关联', on_delete=models.CASCADE, null=True)
    comment = models.CharField('菜单备注', max_length=320,null=True)

    class Meta:
        db_table = 'tb_menu'

    def __str__(self):
        return self.m_name


# 角色菜单表
class RoleMenuModel(BaseModel):
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='角色')
    menu = models.ForeignKey(MenuModel, on_delete=models.CASCADE, verbose_name='菜单')

    class Meta:
        db_table = 'tb_role_menu'

    def __str__(self):
        return self.role


# 地址表
class AddressModel(BaseModel):
    address = models.CharField('地址名称', max_length=20)
    assoc = models.ForeignKey("self", verbose_name='自关联', on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'tb_address'

    def __str__(self):
        return self.address


# 员工表
class UserModel(BaseModel):
    username = models.CharField('员工昵称', max_length=52)

    department = models.ForeignKey(DepartmentModel, on_delete=models.CASCADE, verbose_name='部门')
    rank = models.ForeignKey(RankModel, on_delete=models.CASCADE, verbose_name='职级')
    jobs = models.ForeignKey(JobsModel, on_delete=models.CASCADE, verbose_name='职位', default=None, null=True)

    account = models.CharField('账号', max_length=228, unique=True)
    password = models.CharField('密码', max_length=520)
    phone = models.CharField('手机号', max_length=11, default=True)
    address = models.CharField('地址', max_length=32, null=True)
    birthday = models.DateField('生日')
    state = models.IntegerField('1: 实习生, 2: 正式员工', default=1)
    month_await = models.IntegerField('1: 本月待入职, 2: 本月待离职, 3: 本月待转正', default=None, null=True)
    role = models.ForeignKey(RoleModel, verbose_name='role外键', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.username


# 登录日志表
class LoginLogModel(BaseModel):
    user = models.ForeignKey(UserModel, verbose_name='员工外键', on_delete=models.CASCADE)
    ip = models.CharField('IP地址', max_length=20)
    system = models.CharField('登陆设备', max_length=128)
    places = models.CharField('登陆地址', max_length=128)
    times = models.DateTimeField('登陆时间')

    class Meta:
        db_table = 'tb_login_log'

    def __str__(self):
        return self.system


# 操作日志
class OperationLogModel(BaseModel):
    user = models.ForeignKey(UserModel, verbose_name='员工外键', on_delete=models.CASCADE)
    router = models.CharField('操作路径', max_length=52)
    operation = models.CharField('操作动作', max_length=52)
    times = models.DateTimeField('操作时间')

    class Meta:
        db_table = 'tb_operation_log'

    def __str__(self):
        return self.router


# 申请类型表
class ApplyTypeModel(BaseModel):
    name = models.CharField('申请类型', max_length=32)
    user = models.ForeignKey(UserModel, verbose_name='最后修改人', on_delete=models.CASCADE, default=None, null=True)
    comment = models.CharField('备注', max_length=200, null=True)

    class Meta:
        db_table = 'tb_apply_type'

    def __str__(self):
        return self.name


# 类型分类表
class ApplyCategoryModel(BaseModel):
    apply = models.ForeignKey(ApplyTypeModel, verbose_name='申请类型', on_delete=models.CASCADE)
    leaves_type = models.CharField('类型分类', max_length=52)

    class Meta:
        db_table = 'tb_apply_cate'

    def __str__(self):
        return self.leaves_type


# 车牌号
class CarNumberModel(BaseModel):
    cate = models.ForeignKey(ApplyCategoryModel, verbose_name='申请类型', on_delete=models.CASCADE)
    car_number = models.CharField('车牌号', max_length=52)

    class Meta:
        db_table = 'tb_car_number'

    def __str__(self):
        return self.car_number


# 审批流程表
class FlowModel(BaseModel):
    apply = models.ForeignKey(ApplyTypeModel, verbose_name='申请类型', on_delete=models.CASCADE)
    state = models.IntegerField('状态0: 禁用, 1: 启用')
    process_title = models.CharField('流程说明', max_length=520)

    class Meta:
        db_table = 'tb_flow'

    def __str__(self):
        return self.state

# 申请类型审批人类型表
class ApproverModel(BaseModel):
    apply = models.ForeignKey(ApplyTypeModel, verbose_name='申请类型', on_delete=models.CASCADE)
    user = models.ForeignKey(UserModel, verbose_name='审批人', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_approver'

    def __str__(self):
        return self.apply




# 流程实例表
class ProcessInsModel(BaseModel):
    user = models.ForeignKey(UserModel, verbose_name='申请人', on_delete=models.CASCADE)
    apply = models.ForeignKey(ApplyTypeModel, verbose_name='申请类型', on_delete=models.CASCADE)
    # flow = models.ForeignKey(FlowModel, verbose_name='流程', on_delete=models.CASCADE)
    times = models.DateTimeField('什么时间申请的')
    approver = models.ForeignKey(UserModel, verbose_name='当前审批人', on_delete=models.CASCADE, related_name='pro_user')
    state = models.IntegerField('状态, 0: 未审批, 1: 审批中, 2: 通过, 3: 驳回,4：撤销')
    department = models.ForeignKey(DepartmentModel, verbose_name='部门', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_process_ins'

    def __str__(self):
        return self.times

# 审批节点表
class NodeModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='属于哪个实例', on_delete=models.CASCADE, default=None,
                                null=True)
    user = models.ForeignKey(UserModel, verbose_name='审批人', on_delete=models.CASCADE)
    comment = models.CharField('备注', max_length=200, null=True)
    # order = models.IntegerField('节点顺序')
    times = models.DateTimeField('审批时间')
    state = models.IntegerField('状态, 0: 未审批, 1: 审批中, 2: 通过, 3: 驳回', default=0)

    class Meta:
        db_table = 'tb_node'

    def __str__(self):
        return self.state

# 请假模板表
class LeaveModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    start_time = models.DateTimeField('申请开始的时间')
    end_time = models.DateTimeField('申请结束的时间')
    cause = models.CharField('请假原因', max_length=200, null=True)

    class Meta:
        db_table = 'tb_leave'

    def __str__(self):
        return self.cause


# 借款模板表
class BorrowModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    start_time = models.DateTimeField('申请开始的时间')
    # end_time = models.DateTimeField('申请结束的时间')
    cause = models.CharField('借款原因', max_length=200)
    amount = models.IntegerField("金额")

    class Meta:
        db_table = 'tb_borrow'

    def __str__(self):
        return self.cause


# 出差模板表
class EvectionModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    start_time = models.DateTimeField('申请开始的时间')
    end_time = models.DateTimeField('申请结束的时间')
    cause = models.CharField('出差原因', max_length=200, null=True)
    address = models.CharField('目的地', max_length=200)
    together = models.CharField('同行人', max_length=200, null=True)

    class Meta:
        db_table = 'tb_evection'

    def __str__(self):
        return self.cause


# 转正模板表
class FormalModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    start_time = models.DateTimeField('申请开始的时间')
    cause = models.CharField('自评原因', max_length=200, null=True)

    class Meta:
        db_table = 'tb_formal'

    def __str__(self):
        return self.cause


# 用车模板
class CarModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    start_time = models.DateTimeField('申请开始的时间')
    end_time = models.DateTimeField('申请结束的时间')
    start_address = models.CharField('目的地', max_length=200)
    end_address = models.CharField('目的地', max_length=200)
    car_number = models.ForeignKey(CarNumberModel, verbose_name='车辆id', on_delete=models.CASCADE)


    class Meta:
        db_table = 'tb_car'

    def __str__(self):
        return self.start_time


# 报销模板
class SubmitModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    cause = models.CharField('报销原因', max_length=200)
    amount = models.IntegerField("金额")

    class Meta:
        db_table = 'tb_submit'

    def __str__(self):
        return self.cause


# 上传材料表
class MaterialModel(BaseModel):
    process = models.ForeignKey(ProcessInsModel, verbose_name='流程', on_delete=models.CASCADE, default=None, null=True)
    submit = models.ForeignKey(SubmitModel, verbose_name='报销外键', on_delete=models.CASCADE)
    image = models.CharField('材料图片', max_length=500)

    class Meta:
        db_table = 'tb_submit_image'

    def __str__(self):
        return self.image



# 工资条
class Salary(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    gate = models.IntegerField(verbose_name='出勤天数')
    absent = models.IntegerField(verbose_name='旷工天数')
    leave = models.IntegerField(verbose_name='请假天数')
    base_money = models.IntegerField(verbose_name='基本工资')
    station_money = models.IntegerField(verbose_name='岗位工资')

    class Meta:
        db_table = 'tb_salary'
        verbose_name = '工资'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.gate



# 材料表
class MaterialManageModel(BaseModel):
    m_name = models.CharField('材料名称', max_length=52)
    m_route = models.CharField('材料地址', max_length=520)

    class Meta:
        db_table = 'tb_material_manage'

    def __str__(self):
        return self.m_name


#  #邮件
# class MailModel(BaseModel):
#     user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="发送人")
#     url = models.CharField('邮件地址', max_length=32)
#     consignee = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='receive', verbose_name="收件人")
#     title = models.CharField('标题', max_length=32)
#     content = models.TextField('内容')
#
#     # status = models.SmallIntegerField("0 未读  1 已读", default=0)
#
#     class Meta:
#         db_table = 'tb_mail'
#         verbose_name = '邮件'
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return self.title


# 邮箱表
class Email(BaseModel):
    email = models.CharField('邮件', max_length=32)
    secret_key = models.CharField("密钥", max_length=32, null=True, blank=True)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户", null=True, blank=True)

    class Meta:
        db_table = 'tb_email'
        verbose_name = '邮箱'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.email

# 邮件
class Mail(BaseModel):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="发送人")
    is_read = models.BooleanField("是否已读", default=False)
    url = models.CharField('邮件地址', max_length=32)
    consignee = models.SmallIntegerField("收件人")
    title = models.CharField('标题', max_length=32)
    content = models.CharField('内容', max_length=256)

    class Meta:
        db_table = 'tb_mail'
        verbose_name = '邮件'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 邮件抄送人
class MailCopy(BaseModel):
    mail_msg = models.ForeignKey(Mail, on_delete=models.CASCADE, verbose_name="邮件")
    copy = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="抄送人")

    class Meta:
        db_table = 'tb_mail_copy'
        verbose_name = '邮件抄送人'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.copy